package com.example.shop.goods.dto;

import com.example.shop.common.api.ResponseResult;
import com.example.shop.mapper.GoodsDescriptionPictureMapper;
import com.example.shop.pojo.Goods;
import com.example.shop.pojo.GoodsDescriptionPicture;
import com.example.shop.pojo.GoodsType;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;

import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
@Accessors(chain = true)
@Document(indexName = "goods-detail-browse-info",createIndex = false)
public class GoodsBriefInfo {
    /**
     * 商品的唯一标识
     */
    @Id
    protected Long goodsId;
    /**
     * 该商品归属的商家
     */
    private Long merchantId;
    /**
     * 商品名
     */
    @MultiField(
            mainField = @Field(type = FieldType.Text, analyzer = "ik_max_word",searchAnalyzer = "ik_max_word"),
            otherFields = {
                    @InnerField(suffix = "keyword",type = FieldType.Keyword)
            }
    )
    protected String goodsName;
    /**
     * 商品的类型，比如：手机、衣服
     */
    protected GoodsType goodsType;
    /**
     * 商品的描述图片，比如：商品外观
     */
    @Field(type = FieldType.Keyword)
    protected List<String> goodsDescriptionPicture;
    /**
     * 推荐分值
     * 详细请参考{@link Goods}
     */
    protected long recommendScore;

    public GoodsBriefInfo(long goodsId){
        this.goodsId = goodsId;
    }

    public GoodsBriefInfo setGoodsDescriptionPicture(List<String> goodsDescriptionPicture) {
        ArrayList<String> requestList = new ArrayList<>();
        for(String resource:goodsDescriptionPicture){
            requestList.add(ResponseResult.toRequestPath(resource));
        }
        this.goodsDescriptionPicture = requestList;
        return this;
    }


    @Override
    public String toString() {
        return "GoodsBriefInfo{" +
                "goodsId=" + goodsId +
                ", merchantId=" + merchantId +
                ", goodsName='" + goodsName + '\'' +
                ", goodsType=" + goodsType +
                ", goodsDescriptionPicture=" + goodsDescriptionPicture +
                ", recommendScore=" + recommendScore +
                '}';
    }
    public GoodsBriefInfo assignment(Goods goods,List<String> goodsDescriptionPicture){
        this.setGoodsId(goods.getGoodsId())
                .setMerchantId(goods.getMerchantId())
                .setGoodsName(goods.getGoodsName())
                .setGoodsType(GoodsType.getGoodsType(goods.getGoodsType()))
                .setGoodsDescriptionPicture(goodsDescriptionPicture)
                .setRecommendScore(goods.getRecommendScore());
        return this;
    }
}
